package com.guragura.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.guragura.Guragura;
import com.guragura.game.controller.Level;
import com.guragura.game.model.Player;
import com.guragura.util.Constants;
import com.guragura.util.GuraGameData;
import com.guragura.util.GuraGameInputListener;
import com.guragura.util.GuraMusic;
import com.guragura.util.GuraSound;
import com.guragura.widget.AnimatedSprite;
import com.guragura.widget.ExitDialog;
import com.guragura.widget.KabayanLoadingSprite;
import com.guragura.widget.PauseDialog;

/**
 * Game screen
 * 
 * @author Rendy Bambang Junior
 * 
 */
public class GameScreen extends GuraScreen {

	public Stage stage;
	public PauseDialog pauseDialog;
	public ExitDialog exitDialog;

	public boolean isPauseDialog = false;
	public boolean isExitDialog = false;

	private KabayanLoadingSprite kabayanSprite;
	private AnimatedSprite loadingSprite;
	private AnimatedSprite ttpSprite;
	private TextureRegion background;
	private TextureRegion rememberText;

	private double startTime;
	private double currTime;
	private double deltaTime = 1000;

	public Guragura guragura;
	public GuraSound sound;
	public GuraMusic music;

	/**
	 * current level
	 **/
	public static Level level;

	/**
	 * Player of this game.
	 **/
	private Player player;

	public GameScreen() {
		super();
		// TODO Auto-generated constructor stub
	}

	public GameScreen(Guragura guragura) {
		super(guragura);
		this.guragura = guragura;
	}

	@Override
	public void initFromRes() {
		// GAME SOUND
		sound = guragura.sound;
		music = guragura.music;

		// GAME DATA
		GuraGameData.loadData();

		// CREATE AND LOAD
		loadingSprite = new AnimatedSprite(
				guragura.atlasStart.findRegions(Constants.LOADING_TEXT));
		kabayanSprite = new KabayanLoadingSprite(
				guragura.atlasStart.findRegion(Constants.KABAYAN_LOADING), 3);
		ttpSprite = new AnimatedSprite(
				guragura.atlasInterface
						.findRegions(Constants.TOUCH_TO_PLAY_TEXT));

		loadingSprite.setPosition((320 - loadingSprite.getWidth()) / 2, 10);

		ttpSprite.setPosition((320 - ttpSprite.getWidth()) / 2, 10);

		kabayanSprite.setPosition(
				loadingSprite.getX() + loadingSprite.getWidth(), 10);

		background = guragura.atlasInterface
				.findRegion(Constants.REMEMBER_SCREEN);

		pauseDialog = new PauseDialog(320, 480, guragura);
		exitDialog = new ExitDialog(320, 480, guragura, false);

		rememberText = guragura.atlasInterface.findRegion(
				Constants.REMEMBER_TEXT, 1);

		player = new Player(guragura.atlasInterface);

		level = new Level(this);

		// End of resource load
		// state = SCREEN_RUNNING;
	}

	@Override
	public void initScreen() {
		// TODO Auto-generated method stub
		state = SCREEN_LOADING;
		stage = new Stage(320, 480, true);

		// inputListener = new GuraScreenInputListener(camera, stage);
	}

	@Override
	public void onScreenRunning() {
		level.update(batch, camera);
		guragura.guraFont.draw(player.getLife() + "", 0, 0);	

	}

	@Override
	public void onScreenPause() {
		// TODO Auto-generated method stub
		batch.begin();
		stage.draw();
		batch.end();

	}

	@Override
	public void onScreenLoading() {
		// TODO Auto-generated method stub
		currTime = System.currentTimeMillis();
		batch.begin();
		batch.draw(background, 0, 0);
		kabayanSprite.draw(batch);
		if (currTime - startTime >= deltaTime) {
			if (Gdx.input.justTouched()) {
				state = SCREEN_RUNNING;
			}
			ttpSprite.draw(batch);

		} else {
			loadingSprite.draw(batch);
		}
		if (rememberText != null)
			batch.draw(rememberText, 0, 0);

		batch.end();

	}

	@Override
	public void show() {
		super.show();
		guragura.gameState = Guragura.GAME;
		GuraGameData.loadSavedData();
		Gdx.input.setInputProcessor(new GuraGameInputListener(this));
		startTime = System.currentTimeMillis();
		guragura.music.play();
		
	}

	public Level getLevel() {
		return level;
	}

	public Player getPlayer() {
		return player;
	}

	public AssetManager getAssetManager() {
		return assetManager;
	}

	@Override
	public void exitScreen() {
		// TODO Auto-generated method stub
		if (state == SCREEN_PAUSE) {
			if (!isExitDialog) {
				stage.removeActor(pauseDialog);
				stage.addActor(exitDialog);
				isExitDialog = true;
			} else {
				stage.removeActor(exitDialog);
				isExitDialog = false;
				isPauseDialog = false;
				guragura.popScreen();
			}
		} else {
			guragura.popScreen();			
			guragura.showScore(level.lastScore> player.lastHighScore,level.lastScore,player.lastHighScore);
			
		}
	}

	public void pauseGame() {
		if (!isPauseDialog) {
			isPauseDialog = true;
			state = SCREEN_PAUSE;
			stage.addActor(pauseDialog);
		}
	}

	public void resumeGame() {
		if (isPauseDialog && !isExitDialog) {
			isPauseDialog = false;
			state = SCREEN_RUNNING;
			stage.removeActor(pauseDialog);
		} else if (isPauseDialog && isExitDialog) {
			isExitDialog = false;
			stage.removeActor(exitDialog);
			stage.addActor(pauseDialog);
		}
	}

	public void restartGame() {
		guragura.popScreen();
		guragura.playGame();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		guragura.music.stop();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();		
		guragura.music.play();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
		pauseGame();
		guragura.music.stop();
	}

	public void setToLoading() {
		startTime = System.currentTimeMillis();
		try {
			rememberText = guragura.atlasInterface.findRegion(
					Constants.REMEMBER_TEXT, level.getLevelNum());
		} catch (Exception e) {
			rememberText = guragura.atlasInterface.findRegion(
					Constants.REMEMBER_TEXT, 1);
		}
		state = SCREEN_LOADING;
	}

}
